package com.atguigu.spzx.order.listener;

import com.atguigu.spzx.model.entity.order.OrderInfo;
import com.atguigu.spzx.order.service.OrderInfoService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyOrderStatusListener {
    @Autowired
    OrderInfoService orderInfoService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "orderStatusSuccess.topic",durable = "ture"),
            exchange = @Exchange(name = "order.status.exchange",type = ExchangeTypes.TOPIC),
            key = {"order.success"}
    ))
    public void orderSuccessListener(String orderNo){
        //System.out.println("Order成功服务从mq队列里拿到了"+orderNo);
        //对订单状态进行修改
        int orderStatus=1;  //1为订单待发货状态。
        orderInfoService.updateOrderStatus(orderNo,orderStatus);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "orderStatusFailed.topic",durable = "ture"),
            exchange = @Exchange(name = "order.status.exchange",type = ExchangeTypes.TOPIC),
            key = {"order.failed"}
    ))
    public void orderFailedListener(String orderNo){
        System.out.println("Order失败服务从mq队列里拿到了"+orderNo);
        int orderStatus=-1;  //-1为订单取消
        orderInfoService.updateOrderStatus(orderNo,orderStatus);
    }
}
